<%-- 
    Document   : pesquisaMateriais
    Created on : 08/03/2014, 20:20:53
    Author     : jordan
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Pesquisa Materiais</title>
    </head>
    <body>
        <c:import url="menu.jsp" />
        <h1>Pesquisa Materiais</h1>
        <c:set var="tipoUsuario" value=
            "<%=(
                session.getAttribute("tipoUsuario")
            )%>"/>
        <c:set var="id" value=
            "<%=(
                session.getAttribute("id")
            )%>"/>
        <c:if test="${id==null}">
            <c:redirect url="index.jsp?error=14,W;${id}"/>
        </c:if>
        <c:if test="${tipoUsuario!='3'}">
            <c:redirect url="index.jsp#${id}#"/>
        </c:if>
        <form action="materiais.jsp" method="post">
          <fieldset style=" border: none; width: 85%">
            <table style="width: 80%">
                <tr>
                    <td align="right" style="width: 50%">
                        <button name="retorna" value="retorna">Retornar</button>
                        <button name="insere" value="insere">Inserir</button>
                    </td>
                </tr>
            </table>
          </fieldset>
        </form> 
        <form action="pesquisaMateriais.jsp" method="post">
          <fieldset style="border: 1px solid; width: 85%">
            <legend>Filtros de Pesquisa</legend>
            <table style="width: 80%">
                <tr>
                    <td style="width: 20%">Descrição do Material</td>
                    <td style="width: 80%"><input type="text" id="descricao" name="descricao" style="width: 100%"></td>
                </tr>
                <tr>
                    <td></td><td align="right"><button name="pesquisa" value="pesquisa">Pesquisa</button></td>
                </tr>
            </table>
          </fieldset>
        </form> 
        <c:if test="${param.pesquisa == 'pesquisa'}">
            <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
                               url="jdbc:mysql://localhost/coleta_seletiva"
                               user="root" password="" 
            />
            <sql:query dataSource="${snapshot}" var="result">
                SELECT * from material where descricao LIKE  ?;
                <sql:param value="%${param.descricao}%"/>
            </sql:query>
            <fieldset style="border: 1px solid; width: 85%">
              <legend>Resultado da Pesquisa</legend>
                <table border="1"   style="width: 80%;">
                    <thead>
                        <tr  bgcolor="yellow" align="center">
                            <td>Id</td>
                            <td>Descrição</td>
                            <td>Tempo de decomposição</td>
                            <td>Tóxico</td>
                            <td>Tipo</td>
                            <td>Observações</td>
                            <td>Operações</td>
                        </tr>
                    </thead>
                    <c:if test="${result.rowCount>0}">
                        <c:forEach var="i" begin="0" end="${result.rowCount-1}">
                            <tr>
                                <td><c:out value="${result.rows[i].id}"/></td>
                                <td><c:out value="${result.rows[i].descricao}"/></td>
                                <td><c:out value="${result.rows[i].tempo_decomposicao}"/></td>
                                <td><c:out value="${result.rows[i].toxico}"/></td>
                                <td><c:out value="${result.rows[i].tipo}"/></td>
                                <td><c:out value="${result.rows[i].observacoes}"/></td>
                                <td>
                                    <form action="materiais.jsp" method="post">
                                        <input type="hidden" name="id" value="${result.rows[i].id}" />
                                                            <button name="consulta" value="consulta">Consulta</button>
                                                            <button name="altera" value="altera">Altera</button>
                                                            <button name="exclui" value="exclui">Exclui</button>
                                    </form>
                                </td>
                            </tr>
                        </c:forEach>
                    </c:if>
                </table>
            </fieldset>
        </c:if>
        
    </body>
</html>
